package com.monkeyk.os.web;

import com.alibaba.fastjson.JSONObject;
import com.monkeyk.os.web.api.HighsunPropertyClient;
import com.monkeyk.os.web.api.SysCode;
import org.apache.commons.lang3.StringUtils;
import org.apache.oltu.oauth2.common.OAuth;
import org.apache.oltu.oauth2.common.message.OAuthResponse;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author Shengzhao Li
 */
public abstract class WebUtils extends HighsunPropertyClient {

    private WebUtils() {
    }

    public static void writeOAuthJsonResponse(HttpServletResponse response, OAuthResponse oAuthResponse) {
        final int responseStatus = oAuthResponse.getResponseStatus();
        final Map<String, String> headers = oAuthResponse.getHeaders();
        String message = response.getHeader("message");
        SysCode messageCode=SysCode.INTERFACE_ERROR_CODE;
        String ownerState = response.getHeader("ownerState");
        String userID = response.getHeader("userID");

        for (String key : headers.keySet()) {
            response.addHeader(key, headers.get(key));
        }
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setContentType(OAuth.ContentType.JSON);    //json
        response.setStatus(responseStatus);
        JSONObject toJSON = JSONObject.parseObject(oAuthResponse.getBody());
        if(StringUtils.isNotEmpty(ownerState)){
            toJSON.put("auth_state", Integer.parseInt(ownerState));
        }
        if(StringUtils.isNotEmpty(userID)){
            toJSON.put("user_id",userID);
        }
        if(StringUtils.isNotEmpty(message)){
             messageCode = SysCode.valueOf(message);
        }
        JSONSUCCESS(response, messageCode, toJSON);
    }

    public static void writeOAuthQueryResponse(HttpServletResponse response, OAuthResponse oAuthResponse) {
        final String locationUri = oAuthResponse.getLocationUri();
        try {

            final Map<String, String> headers = oAuthResponse.getHeaders();
            for (String key : headers.keySet()) {
                response.addHeader(key, headers.get(key));
            }

            response.setStatus(oAuthResponse.getResponseStatus());
            response.sendRedirect(locationUri);

        }
        catch (IOException e) {
            throw new IllegalStateException("Write OAuthResponse error", e);
        }
    }

    /**
     * Retrieve client ip address
     *
     * @param request HttpServletRequest
     * @return IP
     */
    public static String retrieveClientIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (isUnAvailableIp(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (isUnAvailableIp(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (isUnAvailableIp(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    private static boolean isUnAvailableIp(String ip) {
        return (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip));
    }
}